//
//  ZYJNavigator.m
//  TravelingByCar
//
//  Created by Wendale on 15-3-3.
//  Copyright (c) 2015年 ZYJ. All rights reserved.
//

#import "ICNavigator.h"

//#import "ICLoginViewController.h"

#import "AppDelegate.h"
#import "AppConstants.h"
#import "ICMacro.h"
#import "UIImage+Addition.h"
#import "ICAccountManager.h"

#import "MALTabBarViewController.h"
#import "ICWelcomePageViewController.h"

#import "BMHomeViewController.h"
#import "BMCategoryViewController.h"
#import "EFQuickPayViewController.h"
#import "ShoppingCartViewController.h"
#import "BMMyViewController.h"
#import "EFLoginViewController.h"
#import "EFQcodePayEndViewController.h"
@interface ICNavigator ()

@end

@implementation ICNavigator


+ (ICNavigator *)sharedInstance {
    static ICNavigator *_instance = nil;
    
    @synchronized (self) {
        if (_instance == nil) {
            _instance = [[self alloc] init];
        }
    }
    
    return _instance;
}

+ (UIViewController *)getCurrentViewController{
    return [self sharedInstance].mainNav.visibleViewController;
}

+ (void)openRootVC
{
    [[self sharedInstance] openRootViewController];
}

+ (void)openLoginWithCompleteBlock:(void (^) (void))block{
    if (APP_CONTEXT.accountManager.accountInfo.token.length > 0) {
        if (block != nil) {
            block();
        }
    }else{
         [[self sharedInstance] openLoginViewControllerWithCompleteBlock:block];
    }
}

- (void)openLoginViewControllerWithCompleteBlock:(void (^) (void))block{
    [EFLoginViewController presentLoginViewController:self.mainNav withCompleteBlock:block];
}

- (void)openRootViewController
{
//    UIImage *navBg = IOS7_OR_LATER ? [UIImage getImageWith:RGBCOLOR(0x141414) size:ccs(kScreenWidth, 64.f)] : [UIImage getImageWith:RGBCOLOR(0x141414) size:ccs(kScreenWidth, 44.f)];
//    [[UINavigationBar appearance] setBackgroundImage:navBg forBarMetrics:UIBarMetricsDefault];
//
//    if ([UINavigationBar instancesRespondToSelector:@selector(setShadowImage:)]){
//        [[UINavigationBar appearance] setShadowImage:[UIImage getImageWith:RGBCOLOR(0x141414) size:CGSizeMake(kScreenWidth, 0.5f)]];
//    }

    /////////////////////
    [self setNavigationBarAppearance];
    AppDelegate* appDelegate = ( AppDelegate*)[[UIApplication sharedApplication] delegate];
    if (![APP_CONTEXT.accountManager hasShowedWelcomeView]) {
        ICWelcomePageViewController *wecome = [[ICWelcomePageViewController alloc] init];
        UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:wecome];
        navigationController.navigationBarHidden = YES;
        appDelegate.window.rootViewController = navigationController;
        return;
    }
    
    ///////////////////
    NSMutableArray *itemsArray = [[NSMutableArray alloc] init];
    NSArray *controllerArray = [NSArray arrayWithObjects:@"BMHomeViewController",@"BMCategoryViewController",@"EFQcodePayEndViewController",@"BMMyViewController", nil];//@"EFCommunityViewController"
    NSArray *normalImageArray = [NSArray arrayWithObjects:@"tab_home",@"tab_ issue",@"nav_icon_quick_default",@"tab_center",nil];//item 正常状态下的背景图片 @"nav_icon_bbs_default"
    NSArray *selectedImageArray = [NSArray arrayWithObjects: @"tab_home_sel",@"tab_ issue",@"nav_icon_quick_selected",@"tab_center_sel",nil];//item被选中时的图片名称 @"nav_icon_bbs_selected"
    NSArray *itemtitleArray = @[@"首页",@"松鼠圈",@"发现",@"我的"];
    
    for (int i = 0; i< controllerArray.count; i++) {
        MALTabBarItemModel *itemModel = [[MALTabBarItemModel alloc] init];
        itemModel.controllerName = controllerArray[i];
        itemModel.itemImageName = normalImageArray[i];
        itemModel.selectedItemImageName = selectedImageArray[i];
        itemModel.itemTitle = itemtitleArray[i];
        [itemsArray addObject:itemModel];
    }
    self.tabBarController = [[MALTabBarViewController alloc] initWithItemModels:itemsArray defaultSelectedIndex:0];
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:self.tabBarController];
    appDelegate.window.rootViewController = navigationController;
    appDelegate.mainNavVC = navigationController;
    self.mainNav = navigationController;
}

+ (UINavigationController *)rootVC
{
    AppDelegate* appDelegate = ( AppDelegate*)[[UIApplication sharedApplication] delegate];
    return (UINavigationController*)appDelegate.window.rootViewController;
}

+ (void)pushViewController:(UIViewController *)vc animated:(BOOL)animated
{
    if ([vc isKindOfClass:[ICBaseViewController class]]) {
        [(ICBaseViewController *)vc setEnableBackButton:YES];
    }
    UINavigationController *nav = [[ICNavigator sharedInstance] mainNav];
    [nav pushViewController:vc animated:animated];
    
}

+ (void)popToRootViewControllerAnimated:(BOOL)animated
{
    [[self rootVC] popToRootViewControllerAnimated:animated];
}

+ (void)presentViewController:(UIViewController *)vc animated:(BOOL)animated
{
    if ([vc isKindOfClass:[ICBaseViewController class]]) {
        [(ICBaseViewController *)vc setEnableBackButton:YES];
    }
    UINavigationController *nav = [[ICNavigator sharedInstance] mainNav];
    
    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];
    [nav presentViewController:nc animated:YES completion:nil];
}

+ (void)removeVC:(UIViewController *)vc
{
    [self removeVC:vc from:[self rootVC]];
}

+ (void)removeVC:(UIViewController *)vc from:(UINavigationController *)nav
{
    NSMutableArray *controllers = [NSMutableArray arrayWithArray:nav.viewControllers];
    [controllers removeObject:vc];
    nav.viewControllers = controllers;
}

- (void)setNavigationBarAppearance
{
    [[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor], NSFontAttributeName:[UIFont systemFontOfSize:18]}];
    [[UINavigationBar appearance] setBarTintColor:[UIColor jc_colorWithHexString:@"#FF654E"]];
//    [[UINavigationBar appearance] setTintColor:[UIColor blackColor]];
//    UIImage *image = [UIImage jc_createImageWithColor:[UIColor jc_colorWithHexString:@"#ffffff"]];
//    [[UINavigationBar appearance] setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
//    UIImage *shadowImage = [UIImage jc_createImageWithColor:[UIColor jc_colorWithHexString:@"#DCDCDC"]];
//    [[UINavigationBar appearance] setShadowImage:shadowImage];
    [[UINavigationBar appearance] setTranslucent:NO];
    [UINavigationBar appearance].layer.masksToBounds = NO;
    
}
@end
